//
//  SHUserAccountViewModel.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/22.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

class SHUserAccountViewModel: NSObject {

    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    var userAccount: SHUserAccount?
    
    static let sharedTools: SHUserAccountViewModel = SHUserAccountViewModel()
    
    var accessToken:String?{
        
        if userAccount?.access_token != nil{
            
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            }else{
                
                return nil
            }
        }else{
            
            return nil
        }
    }
    
    var isLogin: Bool {
        
        return accessToken != nil
    }
    
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
    
}

extension SHUserAccountViewModel{
    
    func saveUserAccount(userAccount:SHUserAccount){
        
        self.userAccount = userAccount;
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
    }
    
    func getUserAccount() -> SHUserAccount?{
        
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? SHUserAccount
        
        return result
    }
}

extension SHUserAccountViewModel{
    
    func loadUserAccount(code: String, isSuccess: (isSuccess
        :Bool)->()){
        
            SHNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> () in
                
                guard let res = response as? [String: AnyObject]
                    else{
                        isSuccess(isSuccess: false)
                        return
                }
                
                let userAccount = SHUserAccount(dict: res)
                self.loadUserInfo(userAccount, isSuceess: isSuccess)
                
                }) { (error) -> () in
                     print("请求失败\(error)")
                    isSuccess(isSuccess: false)
            }
    }
    
    func loadUserInfo(userAccount: SHUserAccount,isSuceess: (isSuceess: Bool)->()){
        
        SHNetworkTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) -> () in
            guard let res = response as? [String: AnyObject] else{
                isSuceess(isSuceess: false)
                
                return
            }
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            print(userAccount)
            
            self.saveUserAccount(userAccount)
            
            isSuceess(isSuceess: true)
             }) { (error) -> () in
                print("请求失败\(error)")
               isSuceess(isSuceess: false)
        }
        
    }
}


